import { Component } from '@angular/core';
import { IonicPage, LoadingController, NavController, NavParams} from 'ionic-angular';

import { ApiProvider } from '../../providers/api/api';
import { CourseDetailPage} from "../course-detail/course-detail";
import {SearchPage} from "../search/search";

/**
 * Generated class for the AllCoursePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-all-course',
  templateUrl: 'all-course.html',
})
export class AllCoursePage {

  allCourseList;//全部课程列表

  searchText="";//搜索内容
  showSearchBar = false;

  // 是否存在继续授课时的padding;
  padding = 0;
  constructor(public navCtrl: NavController, public navParams: NavParams,public http: ApiProvider,public loadingCtrl:LoadingController) {
  }

  /**
   * 当页面加载的时候触发，仅在页面创建的时候触发一次，如果被缓存了，那么下次再打开这个页面则不会触发
   */
  ionViewDidLoad() {
    this.getAllCourseList();
  }

  /**
   * 当进入页面时触发
   */
  ionViewDidEnter()
  {
    this.getAllCourseList();
  }

  /**
   * 全部课程列表
   */
  getAllCourseList()
  {
    this.http.post("/course/getCourseList.do",{bannerType:2,action:'up',requestTime:'now',course_name:this.searchText}).subscribe(item=>{
      if(item["Code"]=="11111111")
      {
        this.http.errorAlert(item["Message"]);
      }
      else{
        this.allCourseList=item["courseList"];
      }
    })
  }

  /**
   * 点击相应课程查看课程详情
   * @param courseId
   */
  showCourseDetails(courseId)
  {
    this.navCtrl.push(CourseDetailPage,{course_id:courseId})
  }

  /**
   * 下拉刷新
   * @param refresher
   */
  doRefresh(refresher) {
    let loading = this.loadingCtrl.create({
      content: '刷新中...',
      showBackdrop: false
    });
    loading.present();

    this.http.post("/course/getCourseList.do",{bannerType:2,action:'up',requestTime:'now',course_name:this.searchText}).subscribe(item=>{
      if(item["Code"]=="11111111")
      {
        loading.dismiss();
        this.http.errorAlert(item["Message"]);
      }
      else{
        setTimeout(() => {
          this.allCourseList=item["courseList"];
          refresher.complete();
          loading.dismiss();
        },2000);
      }
    })
  }

  /**
   * 加载更多
   * @param infiniteScroll
   */
  doInfinite(infiniteScroll)
  {
    let lastTime;
    if(this.allCourseList.length>0)
      lastTime=this.allCourseList[this.allCourseList.length-1].create_time;
    else
      lastTime='now';

    this.http.post("/course/getCourseList.do",{bannerType:2,action:'up',requestTime:'now',course_name:this.searchText}).subscribe(item=>{
      if(item["Code"]=="11111111")
      {
        this.http.errorAlert(item["Message"]);
      }
      else{
        setTimeout(() => {
          this.allCourseList=item["courseList"];
          infiniteScroll.complete();   //当数据请求完成调用
        },500);
      }
    })
  }

  /**
   * 搜索
   * @param ev
   */
  search(ev: any)
  {
    this.showSearchBar = !this.showSearchBar;

    //this.navCtrl.push(SearchPage);

    this.http.post("/course/getCourseList.do",{bannerType:2,action:'up',requestTime:'now',course_name:this.searchText}).subscribe(item=>{
      if(item["Code"]=="11111111")
      {
        this.http.errorAlert(item["Message"]);
      }
      else{
        this.allCourseList=item["courseList"];
      }
    })
  }
}
